package br.com.dataphonebr.selectListener;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.FragmentManager;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import br.com.dataphonebr.R;

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener, OnTimeSetListener {
	
	private TypeDatePicker type;
	private EditText field;
	private final String TIME_SEPARATOR = ":";
	private final String DATE_SEPARATOR = "/";
	private boolean clean;
	private Context context;
	
	public DatePickerFragment(Context context) {
		this.context = context;
	}
	
	public enum TypeDatePicker {
		DATE, TIME;
	}
	
	private Calendar getDateOrTime(){
		final Calendar c = Calendar.getInstance();
		if (field != null && type != null && field.getText() != null && !field.getText().toString().isEmpty()){
			if (type.equals(TypeDatePicker.DATE)){
				String[] dateAux = field.getText().toString().split(DATE_SEPARATOR);
				int day = Integer.parseInt(dateAux[0]);
				int month = Integer.parseInt(dateAux[1]);
				int year = Integer.parseInt(dateAux[2]);
				c.set(Calendar.DATE, day);
				c.set(Calendar.MONTH, month-1);
				c.set(Calendar.YEAR, year);				
			}
			if (type.equals(TypeDatePicker.TIME)){
				String[] dateAux = field.getText().toString().split(TIME_SEPARATOR);
				int hour = Integer.parseInt(dateAux[0]);
				int minute = Integer.parseInt(dateAux[1]);
				c.set(Calendar.HOUR_OF_DAY, hour);
				c.set(Calendar.MINUTE, minute);
			}
		}
		return c;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		clean = false;
		final Calendar c = this.getDateOrTime();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minutes = c.get(Calendar.MINUTE);
		if (type == null || type.equals(TypeDatePicker.DATE)){
			DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
			dialog.setButton(DatePickerDialog.BUTTON_NEGATIVE, getString(R.string.cancel), cleanClick);
			return dialog;
		}
		TimePickerDialog dialog = new TimePickerDialog(getActivity(), this, hour, minutes, true);
		dialog.setButton(DatePickerDialog.BUTTON_NEGATIVE, getString(R.string.cancel), cleanClick);
		return dialog;
	}
	
	DialogInterface.OnClickListener cleanClick = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface arg0, int arg1) {
			if (field != null){
				field.setText("");
				clean = true;
			}
		}
	  };

	@Override
	public void onDateSet(DatePicker view, int year, int month, int day) {
		if (field != null && !clean){
	        field.setText(new StringBuilder().append(day)
	        				.append(DATE_SEPARATOR)
	                        .append(month+1)
	                        .append(DATE_SEPARATOR)
	                        .append(year));
		}
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		if (field != null && !clean){
			Calendar c = Calendar.getInstance();
			c.set(Calendar.HOUR_OF_DAY, hourOfDay);
			c.set(Calendar.MINUTE, minute);
			c.set(Calendar.SECOND, 0);
			SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");  
			String timeFormated = dateFormat.format(c.getTime());  
	        field.setText(timeFormated);
		}
	}
	
	public void showDatePickerDialog(FragmentManager fragmentManager) {
	    DialogFragment newFragment = this;
	    newFragment.show(fragmentManager, context.getString(R.string.select_date_title));
	}

	public void showTimePickerDialog(FragmentManager fragmentManager) {
		DialogFragment newFragment = this;
		newFragment.show(fragmentManager, context.getString(R.string.select_time_title));
	}
	
	public void setType(TypeDatePicker type) {
		this.type = type;
	}
	
	public void setField(EditText field) {
		this.field = field;
	}
}